<template>
	<div class="select-none loading">
		<img :src="bg" class="wave" />
		<div class="login-container">
			<div class="img">
				<component :is="toRaw(illustration)" />
			</div>
			<div class="login-box">
				<div class="login-form">
					<img style="width: 80px; height: 80px" src="../assets/logo.png" />
					<h2 class="outline-none">臻东C</h2>
					<el-form ref="ruleFormRef" :model="ruleForm" size="large" :rules="rules">
						<el-form-item
							:rules="[
								{
									required: true,
									message: '请输入账号',
									trigger: 'blur'
								}
							]"
							prop="username"
						>
							<el-input clearable v-model="ruleForm.username" placeholder="账号" />
						</el-form-item>

						<el-form-item prop="password">
							<el-input clearable show-password v-model="ruleForm.password" placeholder="密码" />
						</el-form-item>
						<el-button class="w-full mt-4" size="default" type="primary" :loading="loading" @click="onLogin(ruleFormRef)">
							登录
						</el-button>
					</el-form>
				</div>
			</div>
		</div>
	</div>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router"
// import {message} from "@/utils/message";
// import {loginRules} from "./utils/rule";
// import {useNav} from "@/layout/hooks/useNav";
import type { FormInstance } from "element-plus"
// import {useLayout} from "@/layout/hooks/useLayout";
// import {useUserStoreHook} from "@/store/modules/user";
// import {initRouter, getTopMenu} from "@/router/utils";
// import {bg, avatar, illustration} from "./utils/static";
// import {useRenderIcon} from "@/components/ReIcon/src/hooks";
import { ref, reactive, toRaw, onMounted, onBeforeUnmount } from "vue"
// import {useDataThemeChange} from "@/layout/hooks/useDataThemeChange";

import dayIcon from "/@/assets/svg/day.svg"
import darkIcon from "/@/assets/svg/dark.svg"
// import Lock from "/@iconify-icons/ri/lock-fill";
// import User from "@iconify-icons/ri/user-3-fill";
import bg from "/@/assets/svg/bg.png"
import avatar from "/@/assets/svg/avatar.svg?component"
import illustration from "/@/assets/svg/illustration.svg?component"
import { buildValidatorData } from "/@/utils/validate"
import { login } from "/@/api"
import { useAdminInfo } from "/@/stores/adminInfo"

defineOptions({
	name: "Login"
})

const adminInfo = useAdminInfo()
const state = reactive({
	showCaptcha: false,
	submitLoading: false
})
const router = useRouter()
const loading = ref(false)
const ruleFormRef = ref<FormInstance>()

const rules = reactive({
	username: [buildValidatorData({ name: "required", message: "请输入帐户" }), buildValidatorData({ name: "account" })],
	password: [buildValidatorData({ name: "required", message: "请输入密码" }), buildValidatorData({ name: "password" })]
})

const ruleForm = reactive({
	username: "",
	password: ""
})

const onLogin = async (formEl: FormInstance | undefined) => {
	loading.value = true
	if (!formEl) return
	await formEl.validate((valid, fields) => {
		if (valid) {
			login("post", ruleForm)
				.then((res) => {
					adminInfo.dataFill(res.data.userinfo)
					router.push({ path: "/" })
				})
				.finally(() => {
					state.submitLoading = false
					loading.value = false
				})
		} else {
			loading.value = false
			return fields
		}
	})
}

/** 使用公共函数，避免`removeEventListener`失效 */
function onkeypress({ code }: KeyboardEvent) {
	if (code === "Enter") {
		onLogin(ruleFormRef.value)
	}
}

onMounted(() => {
	window.document.addEventListener("keypress", onkeypress)
})

onBeforeUnmount(() => {
	window.document.removeEventListener("keypress", onkeypress)
})
</script>

<style scoped>
@import url("../styles/login.css");
</style>

<style lang="scss" scoped>
.select-none {
	background-color: #ffffff;
}

:deep(.el-input-group__append, .el-input-group__prepend) {
	padding: 0;
}
</style>
